package cn.nexd.app.collector.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.Transformation;
import android.widget.Button;
import android.widget.TextView;

import cn.nexd.app.collector.R;

/**
 * Created by xunzongxia on 16/9/23.
 */

public class CollectorEndDialog extends Dialog implements View.OnClickListener {

    private final AnimationSet mModalInAnim;
    private final AnimationSet mModalOutAnim;
    private final Animation mOverlayOutAnim;
    private Button btnOk;
    private Button btnCancel;
    private TextView tvMsg;
    private View dialogView;
    private String dialogMsg;
    private onDialogClickListener onDialogClickListener;

    public CollectorEndDialog(Context context) {
        super(context);
        mModalInAnim = (AnimationSet) OptAnimationLoader.loadAnimation(getContext(), R.anim.modal_in);
        mModalOutAnim = (AnimationSet) OptAnimationLoader.loadAnimation(getContext(), R.anim.modal_out);
        mModalOutAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dialogView.setVisibility(View.GONE);
                dialogView.post(new Runnable() {
                    @Override
                    public void run() {
                        CollectorEndDialog.super.dismiss();
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        // dialog overlay fade out
        mOverlayOutAnim = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                WindowManager.LayoutParams wlp = getWindow().getAttributes();
                wlp.alpha = 1 - interpolatedTime;
                getWindow().setAttributes(wlp);
            }
        };
        mOverlayOutAnim.setDuration(120);
    }

    public void setOnDialogClickListener(CollectorEndDialog.onDialogClickListener onDialogClickListener) {
        this.onDialogClickListener = onDialogClickListener;
    }

    public void setDialogMsg(String dialogMsg) {
        this.dialogMsg = dialogMsg;
        tvMsg.setText(dialogMsg);
    }


    public interface  onDialogClickListener{
        void onSave();
        void onFail();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_collect_error);
        dialogView = getWindow().getDecorView().findViewById(android.R.id.content);
        btnOk= (Button) findViewById(R.id.dialog_btn_ok);
        btnCancel= (Button) findViewById(R.id.dialog_btn_cancel);
        tvMsg= (TextView) findViewById(R.id.tv_msg_string);
        btnOk.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        dialogView.startAnimation(mModalInAnim);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dialog_btn_ok:
                if(isShowing()){
                    dismiss();
                }
                if(onDialogClickListener!=null){
                    onDialogClickListener.onSave();
                }
                break;
            case R.id.dialog_btn_cancel:
                if(isShowing()){
                    dismiss();
                }
                if(onDialogClickListener!=null){
                    onDialogClickListener.onFail();
                }
                break;
        }
    }
}

